<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/04/16
 * Time: 下午3:39
 * 类名：EmaySmsApi
 * 功能：亿美软通短信平台接口请求类
 * 详细：构造亿美软通短信平台短信接口请求，获取远程HTTP数据
 */

namespace Org\Emay;


class EmaySmsApi {

    //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    //创蓝发送短信接口URL, 如无必要，该参数可不用修改
    protected $api_send_url = 'http://sdk999ws.eucp.b2m.cn:8080/sdkproxy/sendsms.action';

    //创蓝短信余额查询接口URL, 如无必要，该参数可不用修改
    protected $api_balance_query_url = 'http://sdk999ws.eucp.b2m.cn:8080/sdkproxy/querybalance.action';

    //创蓝账号 替换成你自己的账号
    protected $api_cdkey = '9SDK-EMY-0999-JFUPL';

    //创蓝密码 替换成你自己的密码
    protected $api_password = '442803';

    //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑



    /**
     * 发送短信
     *
     * @param string $mobile 手机号码
     * @param string $msg 短信内容
     * @param string $addserial 附加码
     */
    public function sendSMS( $mobile, $msg, $addserial = '') {

        //创蓝接口参数
        $postArr = array (
            'cdkey' => $this->api_cdkey,
            'password' => $this->api_password,
            'message' => $msg,
            'phone' => $mobile,
            'addserial' => $addserial
        );

        $result = $this->curlPost( $this->api_send_url , $postArr);

        return $result;
    }

    /**
     * 查询额度
     *
     *  查询地址
     */
    public function queryBalance() {
        global $chuanglan_config;
        //查询参数
        $postArr = array (
            'account' => $this->api_account,
            'pswd' => $this->api_password
        );
        $result = $this->curlPost( $this->api_balance_query_url, $postArr);
        return $result;
    }

    /**
     * 处理返回值
     *
     */
    public function execResult($result){

        $result = trim($result);
        $xml = simplexml_load_string($result);
        $result = json_decode(json_encode($xml),TRUE);

        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     */
    private function curlPost($url,$postFields){

        $postFields = http_build_query($postFields);

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_POST, 0 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        return $result;
    }

    //魔术获取
    public function __get($name){
        return $this->$name;
    }

    //魔术设置
    public function __set($name,$value){
        $this->$name=$value;
    }
}